;
(function() {

    var lottery = {
        index: -1, //当前转动到哪个位置，起点位置
        count: 0, //总共有多少个位置
        timer: 50, //setTimeout的ID，用clearTimeout清除
        speed: 50, //初始转动速度
        times: 40, //转动次数
        cycle: 50, //转动基本次数：即至少需要转动多少次再进入抽奖环节
        prize: -1, //中奖位置
        allTimes: 0,
        ChouJianglength: 12,
        init: function(id) {
            if ($("#" + id).find(".lottery-unit").length > 0) {
                $lottery = $("#" + id);
                $units = $lottery.find(".lottery-unit");
                this.obj = $lottery;
                this.count = $units.length;
                $lottery.find(".lottery-unit-" + this.index).addClass("active");
            };
        },
        roll: function() {
            var index = this.index;
            var count = this.count;
            var lottery = this.obj;
            $(lottery).find(".lottery-unit-" + index).removeClass("active");
            index += 1;
            if (index > count - 1) {
                index = 0;
            };
            $(lottery).find(".lottery-unit-" + index).addClass("active");
            this.index = index;
            return false;
        },
        stop: function(index) {
            this.prize = index;
            return false;
        },
        watch: function(pro, callback) {
            if (pro in this) {
                var old = this[pro];
                Object.defineProperty(this, pro, {
                    set: function(val) {
                        var o = old;
                        old = val;
                        callback(val, o, this);
                    },
                    get: function() {
                        return old;
                    }
                });
            } else {
                throw new Error("no such property");
            }
        },
        toString: function() {
            var str = "{   ";
            for (pro in this) {
                if (typeof this[pro] !== "function")
                    str += (pro + " : " + lottery[pro] + ",")
            }
            str[str.length - 1] = ' ';
            str += " }";
            return str;
        },
        quanzi: { "data": [{ "addMoney": 1, "category": "类目7", "cost": 30, "giftId": "Z56128", "giftName": "波棒糖", "id": 27, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/056d46ab628d4f45a146ccb095010614.png", "index": 0 }, { "addMoney": 1, "category": "类目8", "cost": 50, "giftId": "Z95278", "giftName": "音你而在", "id": 28, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/2c076f0c7b294571acc1562064474a38.png", "index": 1 }, { "addMoney": 1, "category": "类目9", "cost": 50, "giftId": "Z59784", "giftName": "项链", "id": 29, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/85c30c723dc945ebb432c6cbbe480e16.png", "index": 2 }, { "addMoney": 1, "category": "不中奖的礼物", "cost": 100, "giftId": "Z20008", "giftName": "耳环", "id": 30, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/1907ca29056747849641f8cb5677ea72.png", "index": 3 }, { "addMoney": 1, "category": "类目12", "cost": 200, "giftId": "Z65381", "giftName": "黄金叶", "id": 32, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/3150063572ea4b0fb77c9f88140cbbec.png", "index": 4 }, { "addMoney": 1, "category": "类目13", "cost": 300, "giftId": "Z77027", "giftName": "爱心猪", "id": 33, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/321604a16b234848ab546e85c1724b26.png", "index": 11 }], "data2": [{ "addMoney": 1, "category": "类目14", "cost": 500, "giftId": "Z71201", "giftName": "Papty", "id": 37, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/6d4c45b0630a42c88dcfc510faeaaba2.png", "index": 5 }, { "addMoney": 1, "category": "类目15", "cost": 2000, "giftId": "Z90897", "giftName": "TT", "id": 38, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/a014ce7aa8ae49d8ab854fc17b97359b.png", "index": 10 }, { "addMoney": 1, "category": "类目16", "cost": 10000, "giftId": "Z53936", "giftName": "粑粑", "id": 39, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/0aa2b45f08ef458eac1e8daa02779c03.png", "index": 9 }, { "addMoney": 1, "category": "类目18", "cost": 52000, "giftId": "Z86182", "giftName": "心动", "id": 40, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/7ebe46bbb4514a6dbe005e646b55cce1.png", "index": 8 }, { "addMoney": 1, "category": "不中奖的礼物", "cost": 131400, "giftId": "Z96247", "giftName": "表白弹幕", "id": 41, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/5d02dd63830046c19aeb114e20c04c47.jpg", "index": 7 }, { "addMoney": 1, "category": "类目17", "cost": 200000, "giftId": "Z90003", "giftName": "告白气球", "id": 44, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/add30fd1368744ca9d65c0a58e56d0e7.png", "index": 6 }] },
        trChooseImgHtml: function() {
            console.log(this.quanzi);

            var html = template('showChooseImgs', this.quanzi);
            $('.trChooseImg').html(html);
        }
    };
    var click = false;

    function roll() {
        lottery.times += 1;
        lottery.roll(); //转动过程调用的是lottery的roll方法，这里是第一次调用初始化
        if (lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index) {
            clearTimeout(lottery.timer);
            // 监听allTimes:
            lottery.allTimes = lottery.prize;
            lottery.prize = -1;
            lottery.times = 0;
            click = false;
        } else {
            if (lottery.times < lottery.cycle) {
                lottery.speed -= 10;
            } else if (lottery.times == lottery.cycle) {
                // var index = Math.random() * (lottery.count) | 0;
                // lottery.prize = index;
            } else {
                if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) {
                    lottery.speed += 110;
                } else {
                    lottery.speed += 20;
                }
            }
            if (lottery.speed < 40) {
                lottery.speed = 40;
            };
            console.log(lottery.speed);

            lottery.timer = setTimeout(roll, lottery.speed); //循环调用
        }
        return false;
    }

    $(function() {
        lottery.trChooseImgHtml();
        var lotteryTimer = setTimeout(function() {
            lottery.init('lottery');
            $("#lottery .inputButton").click(function(e) {
                //click控制一次抽奖过程中不能重复点击抽奖按钮，后面的点击不响应
                if (click) {
                    return false;
                } else {
                    clickInput();
                    $('#lottery .inputButton').addClass('disable');
                }
            });

            function clickInput(index) {
                if (index === "1") {
                    var str = $('#lottery .inputButton .inputButton1').val();
                    str = str.substr(1, str.length - 2);
                } else if (index === "2") {
                    var str = $('#lottery .inputButton .inputButton2').val();
                    str = str.substr(1, str.length - 2);
                }
                starXuanZhuan();

                function starXuanZhuan() {
                    lottery.count = lottery.ChouJianglength;
                    count = lottery.ChouJianglength;
                    lottery.speed = 120;
                    lottery.timer = 70;
                    lottery.times = 50;
                    // 抽中时执行事件：
                    // lotteryLuckyDraw();
                    var index = Math.random() * (lottery.count) | 0;
                    var prize = index;
                    lottery.prize = prize;

                    lottery.watch("allTimes", function(n, o, _this) {
                        if (_this.prize == prize) {
                            var manyPrizeTimer = setTimeout(function() {
                                console.log(_this);
                                $('#lottery .inputButton').removeClass('disable');
                                clearTimeout(manyPrizeTimer);
                            }, 300)
                        }
                    });

                    roll(); //转圈过程不响应click事件，会将click置为false
                    click = true; //一次抽奖完成后，设置click为true，可继续抽奖
                    // var inputButtonTimer = setTimeout(function() {
                    //     $('#lottery .inputButton').removeClass('disable');
                    //     clearTimeout(inputButtonTimer);
                    // }, 3000)
                    // 余额不足时调用：
                    return false;
                }
            }

            clearTimeout(lotteryTimer);
        }, 400)
    })

})(window, document)